package model.net
{
	import flash.net.Socket;
	import flash.utils.ByteArray;
	
	import model.card.ICardData;
	import model.room.FriendData;
	import model.room.MailData;

	public class NetRomote
	{
		//发送名人名言
		public function sendFamous(content:String):void
		{
			sendClientMgs(NetDataType.CS_FAMOUS_COMMENT,content,"","");
		}
		
		//请求座位详细信息
		public function sitInfoDetail(sitId:String):void
		{
			sendClientMgs(NetDataType.CS_PLAYER_INFO,sitId,"","");
		}
		
		//赠送道具
		public function sendItem(sitId:String,itemId:String,count:String):void
		{
			sendClientMgs(NetDataType.CS_SEND_ITEM,sitId,itemId,count);
		}
		
		//查看任务
		public function showTask():void
		{
			sendClientMgs(NetDataType.CS_GAME_TASK_QUERY,"","","");
		}
		
		//使用物品
		public function useItem(id:String):void
		{
			sendClientMgs(NetDataType.CS_USE_ITEM,id,"","");
		}
		
		
		//物品列表
		public function itemList(type:String):void
		{
			sendClientMgs(NetDataType.CS_ITEM_LIST,type,"","");
		}
		
		//托管
		public function sendAuto(type:String):void
		{
			sendClientMgs(NetDataType.CS_SWITCH_AUTO,type,"","");
		}
		
		
		//游戏反馈
		public function getFeed(snsKey:String,content:String,type:int,imgBA:ByteArray,snsId:String):ByteArray
		{
			b.length = 0;
			writeMessage(NetDataType.AC_FEED);
			writeString(snsKey);
			writeShort(type);
			writeString(content);
			writeInt(imgBA.length);
			b.writeBytes(imgBA,0,imgBA.length);
			writeString(snsId);
			var ba:ByteArray = new ByteArray();
			ba.writeBytes(b,0,b.length);
			
			b.length = 0;
			return ba;
		}
		
		
		//请求返回大厅
		public function goHall():void
		{
			sendClientMgs(NetDataType.CS_GO_ROOM,"","","");
		}
		
		//删除好友
		public function sendRemoveFriend(datas:Array):void
		{
			writeMessage(NetDataType.CS_REMOVE_FRIEND);
			writeShort(datas.length);
			
			var data:FriendData;
			for (var i:int=0; i<datas.length; i++)
			{
				data = datas[i];
				writeInt(data.playerId);
			}
			flush();
		}
		
		//请求好友列表
		public function queryFriendList():void
		{
			sendClientMgs(NetDataType.CS_QUERY_FRIEND,"","","");
		}

		//提交申请应答
		public function sendFriendApplyResult(datas:Array):void
		{
			writeMessage(NetDataType.CS_FRIEND_RESULT);
			writeShort(datas.length);
			var data:MailData;
			for (var i:int=0; i<datas.length; i++)
			{
				data = datas[i];
				writeShort(data.result);
				writeInt(int(data.inviteId));
				trace("invite:",data.result);
			}
			flush();
		}
		//添加好友申请
		public function sendFriendApply(sitId:String):void
		{
			sendClientMgs(NetDataType.CS_FRIEND_APPLY,sitId,"","");
		}


		//系统提示面板选择
		public function sendPromptSelect(id:String,isYesOrNo:int):void
		{
			sendClientMgs(NetDataType.CS_CONFIRM_DIALOG,id,isYesOrNo.toString(),"");
		}
		
		
		//玩家站起
		public function sendStandUp():void
		{
			sendClientMgs(NetDataType.CS_STAND_UP,"","","");
		}

		//发表情	
		public function sendFace(faceKey:String):void
		{
			sendClientMgs(NetDataType.CS_CHAT_FACE,faceKey,"","");
		}
		
		public function sendGameLogin(snsId:String,snsName:String,sessionKey:String,roomId:String,sns:String,isQuick:int):void
		{
			writeMessage(NetDataType.CS_GAME_LOGIN);
			writeString(snsId);
			writeString(snsName);
			writeString(sessionKey);
			writeString(roomId);
			writeString(sns);
			writeShort(isQuick);
			flush();
		}
		
		public function sendSitDown(sitId:int):void
		{
			sendClientMgs(NetDataType.CS_SIT_DOWN,sitId.toString(),"","");
		}
		
		//玩家出牌
		public function sendPlayCards(cardDatas:Array):void
		{
			writeMessage(NetDataType.CS_PLAY_CARDS);
			writeShort(cardDatas.length);
			var cardData:ICardData;
			for (var i:String in cardDatas)
			{
				cardData = cardDatas[i];
				writeShort(cardData.id)
				writeShort(cardData.value);
			}
			flush();
		}
		
		//玩家过牌
		public function sendPlayPass():void
		{
			sendClientMgs(NetDataType.CS_PASS_CARDS,"","","");
		}
		
		//聊天发送
		public function sendChatMsg(content:String):void
		{
			sendClientMgs(NetDataType.CS_CHAT_MSG,content,"","");
		}
		
		//心跳
		public function sendHeartBeat():void
		{
			writeMessage(NetDataType.CS_HEART_BEAT);
			flush();
		}
		
//////////////////////////////////////////////////////////////////////////////////////////////////////////		
		private var socket:Socket = NetSocket.getInstance().socket;
		private var b:ByteArray = new ByteArray();
		
		private function writeInt(i:int):void
		{
			b.writeInt(i);
		}
		
		private function writeShort(v:int):void
		{
			b.writeShort(v);
		}
		
		private function writeString(str:String):void
		{
			var byteArray:ByteArray = new ByteArray();
			byteArray.writeMultiByte(str,"GBK");
			var length:int = byteArray.length;
			b.writeShort(length);
			byteArray.position = 0;
			b.writeBytes(byteArray,0,byteArray.bytesAvailable);
		}
		
		private function writeMessage(type:int):void
		{
			b.writeShort(type);
		}
		
		private function sendClientMgs(type:int,p1:String,p2:String,p3:String):void
		{
			writeMessage(NetDataType.CS_CLIENT_MESSAGE);
			writeMessage(type);
			writeString(p1);
			writeString(p2);
			writeString(p3);
			flush();
		}

		private function flush():void
		{
			try
			{
				b.position = 0;
				socket.writeShort(b.bytesAvailable);
				socket.writeBytes(b,0,b.bytesAvailable);
				socket.flush();
				b.length = 0;
			}
			catch(error:Error)
			{
				trace("socket write Error!!!!!");
			}
		}
		
		public function clear():void
		{
			b.length = 0;
		}
	}
}